package guardian.web

import com.mls.guardian.common.JsonUtil
import vo.User

class LoginController {

    def userService

    def index() {
        if(!session.loginUser){
            result.suc = false;
            result.message = "没有登录";
        }
        render(contentType:"text/json") {
            result
        }
    }

    def login() {
        def user = JsonUtil.deserialize(params.content)
        String userName = user.userName
        String password  = user.password
        User loginUser =  userService.login(userName,password)
        if(loginUser == null){
            result.suc = false
            result.message = "用户名，或者密码不正确！"
        }else{
            result.suc = true
        }
        render(contentType:"text/json") {
            session.setAttribute("loginUser",loginUser)
            result
        }
    }

    def Map result = ['suc':true,'message':'']
    def logout(){
        session.removeAttribute("loginUser")
        render(contentType:"text/json") {
            result.suc = false
            result
        }
    }

}
